home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / BARSDI.PAK / STATBAR.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  10KB  |  346 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995 Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: statbar.c
  9. //
  10. //  PURPOSE: Handles general routines for the Barsdi sample.
  11. //
  12. //  FUNCTIONS:
  13. //    MsgTimer      - Handles the WM_TIMER messages to set the time on
  14. //                    the status bar.
  15. //    MsgMouseMove  - Handles the WM_MOUSEMOVE to display the cursor position.
  16. //    MsgMenuSelect - Handle the WM_MENUSELECT message. This message will
  17. //                    enable the status bar control to update when the user
  18. //                    moves across menu items on the main window.
  19. //    InitializeStatusBar - Sets the pane positions in the statusbar control
  20. //    CreateSBar    - Calls CreateStatusWindow() to create the status bar
  21. //    UpdateStatusBar - Updates the statusbar control with appropriate text
  22. //
  23.  
  24. #include <windows.h>            // required for all Windows applications
  25. #include <windowsx.h>
  26. #include <commctrl.h>           // prototypes and defs for common controls
  27. #include "globals.h"            // prototypes specific to this application
  28. #include "statbar.h"            // prototypes specific to statbar.c
  29. #include "resource.h"
  30.  
  31.  
  32. // Global Variables for the status bar control.
  33.  
  34. HWND  hWndStatusbar;
  35.  
  36. //  **TODO**  Add entries to the string table in barsdi.rc for each menu
  37. //            command.  MsgMenuSelect (below) loads these strings to display
  38. //            information in the status bar.  MsgMenuSelect assumes that the
  39. //            string ID is the same as the command ID and that a string
  40. //            exists for every command.
  41. //
  42. // The following array contains resource string ID's for popup menus
  43. // in the main application menu.  This array is used by MsgMenuSelect
  44. // to display information in the status bar.
  45. //
  46. //  **TODO**  Add entries to this array for each popup menu in the same
  47. //            positions as they appear in the main menu.  Remember to define
  48. //            the ID's in globals.h and add the strings to barsdi.rc.
  49.  
  50. UINT idPopup[] =
  51. {
  52.     IDS_FILEMENU,
  53.     IDS_EDITMENU,
  54.     IDS_HELPMENU,
  55. };
  56.  
  57.  
  58. //
  59. //  FUNCTION: MsgTimer(HWND, UINT, WPARAM, LPARAM)
  60. //
  61. //  PURPOSE: Calls GetLocalTime() to set the time on the status bar
  62. //
  63. //
  64. //  PARAMETERS:
  65. //
  66. //    hwnd      - Window handle  (Unused)
  67. //    uMessage  - Message number (Unused)
  68. //    wparam    - Extra data     (Unused)
  69. //    lparam    - Extra data     (Unused)
  70. //
  71. //  RETURN VALUE:
  72. //
  73. //    Always returns 0 - Message handled
  74. //
  75. //  COMMENTS:
  76. //
  77. //    Every time the window procedure receives a Timer message, it calls
  78. //    GetLocalTime() to obtain the time and then formats the time into
  79. //    a string. The time sting is then displayed on the status bar.
  80. //
  81.  
  82. #pragma argsused
  83. LRESULT MsgTimer(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  84. {
  85.     char        szBuf[16];      // Temp buffer.
  86.     SYSTEMTIME  sysTime;
  87.  
  88.  
  89.     GetLocalTime(&sysTime);
  90.  
  91.     wsprintf(szBuf,
  92.              "%2d:%02d:%02d %s",
  93.              (sysTime.wHour == 0 ? 12 :
  94.              (sysTime.wHour <= 12 ? sysTime.wHour : sysTime.wHour -12)),
  95.              sysTime.wMinute,
  96.              sysTime.wSecond,
  97.              (sysTime.wHour < 12 ? "AM":"PM"));
  98.  
  99.     UpdateStatusBar(szBuf, 4, 0);
  100.     return 0;
  101. }
  102.  
  103.  
  104. //
  105. //  FUNCTION: MsgMousemove(HWND, UINT, WPARAM, LPARAM)
  106. //
  107. //  PURPOSE:  Obtains the Cursor position to display coordinates on
  108. //            the staus bar.
  109. //
  110. //  PARAMETERS:
  111. //
  112. //    hwnd      - Window handle  (Unused)
  113. //    uMessage  - Message number (Unused)
  114. //    wparam    - Extra data     (Unused)
  115. //    lparam    - Extra data     (Used)
  116. //
  117. //  RETURN VALUE:
  118. //
  119. //    Always returns 0 - Message handled
  120. //
  121. //  COMMENTS:
  122. //
  123. //    The mouse coordinates (x and y) are in the HI And LO words
  124. //    of LPARAM
  125. //
  126. //
  127.  
  128. #pragma argsused
  129. LRESULT MsgMousemove(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  130. {
  131.      char szBuf[20];             // Array for formatting mouse coordinates
  132.  
  133.      wsprintf(szBuf, "%d,%d", LOWORD(lparam), HIWORD(lparam));
  134.      UpdateStatusBar(szBuf, 2, 0);
  135.      return 0;
  136. }
  137.  
  138.  
  139. //
  140. //  FUNCTION: MsgMenuSelect(HWND, UINT, WPARAM, LPARAM)
  141. //
  142. //  PURPOSE:  Upadates menu selections on the staus bar.
  143. //
  144. //
  145. //  PARAMETERS:
  146. //
  147. //    hwnd      - Window handle  (Used)
  148. //    uMessage  - Message number (Used)
  149. //    wparam    - Extra data     (Used)
  150. //    lparam    - Extra data     (Used)
  151. //
  152. //  RETURN VALUE:
  153. //
  154. //    Always returns 0 - Message handled
  155. //
  156. //  COMMENTS:
  157. //    This message is sent when the user selects menu items by
  158. //    by pulling down  a popup menu move the mouse around to highlite
  159. //    different menu items.
  160. //
  161. //
  162.  
  163. #pragma argsused
  164. LRESULT MsgMenuSelect(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  165. {
  166.      static char szBuffer[128];
  167.      UINT   nStringID;
  168.      UINT   fuFlags = GET_WM_MENUSELECT_FLAGS(wparam, lparam) & 0xffff;
  169.      UINT   uCmd    = GET_WM_MENUSELECT_CMD(wparam, lparam);
  170.      HMENU  hMenu   = GET_WM_MENUSELECT_HMENU(wparam, lparam);
  171.  
  172.  
  173.      szBuffer[0] = 0;                            // First reset the buffer
  174.  
  175.  
  176.      if (fuFlags == 0xffff && hMenu == NULL)     // Menu has been closed
  177.           nStringID = IDS_DESCRIPTION;
  178.  
  179.      else if (fuFlags & MFT_SEPARATOR)           // Ignore separators
  180.           nStringID = 0;
  181.  
  182.      else if (fuFlags & MF_POPUP)                // Popup menu
  183.      {
  184.           if (fuFlags & MF_SYSMENU)               // System menu
  185.                 nStringID = IDS_SYSMENU;
  186.  
  187.           else
  188.                 // Get string ID for popup menu from idPopup array.
  189.                 nStringID = ((uCmd < sizeof(idPopup)/sizeof(idPopup[0])) ?
  190.                                      idPopup[uCmd] : 0);
  191.      }  // for MF_POPUP
  192.  
  193.      else                                        // Must be a command item
  194.           nStringID = uCmd;                       // String ID == Command ID
  195.  
  196.      // Load the string if we have an ID
  197.      if (0 != nStringID)
  198.           LoadString(hInst, nStringID, szBuffer, sizeof(szBuffer));
  199.  
  200.      // Finally... send the string to the status bar
  201.      UpdateStatusBar(szBuffer, 0, 0);
  202.  
  203.     return 0;
  204. }
  205.  
  206.  
  207. //
  208. //  FUNCTION: InitializeStatusBar(HWND)
  209. //
  210. //  PURPOSE:  Initialize statusbar control with time and mouse positions.
  211. //
  212. //
  213. //  PARAMETERS:
  214. //
  215. //  hwndParent - Window handle of the status bar's parent
  216. //
  217. //
  218. //  RETURN VALUE:  NONE
  219. //
  220. //
  221. //  COMMENTS:
  222. //
  223. //   This function initializes the time  and mouse positions sections of
  224. //   the statubar window. The Date for the time section is obtained by
  225. //   calling SetTimer API. When the timer messages start comming in,
  226. //   GetSytemTime() to fill the time section.
  227. //   The WPARAM of SB_SETTEXT is divided into 2 parameters. The LOWORD
  228. //   determines which section/part the text goes into, and the HIWORD
  229. //   tells how the bar is drawn (popin or popout).
  230. //
  231.  
  232. void InitializeStatusBar(HWND hwndParent)
  233. {
  234.     const cSpaceInBetween = 8;
  235.     int   ptArray[5];   // Array defining the number of parts/sections
  236.     SIZE  size;         // the Status bar will display.
  237.     RECT  rect;
  238.     HDC   hDC;
  239.  
  240.    /*
  241.     * Fill in the ptArray...
  242.     */
  243.  
  244.     hDC = GetDC(hwndParent);
  245.     GetClientRect(hwndParent, &rect);
  246.  
  247.     ptArray[4] = rect.right;
  248.  
  249.     if (GetTextExtentPoint(hDC, "00:00:00 PM", 12, &size))
  250.         ptArray[3] = ptArray[4] - (size.cx) - cSpaceInBetween;
  251.     else
  252.         ptArray[3] = 0;
  253.  
  254.     if (GetTextExtentPoint(hDC, "Time:", 6, &size))
  255.         ptArray[2] = ptArray[3] - (size.cx) - cSpaceInBetween;
  256.     else
  257.         ptArray[2] = 0;
  258.  
  259.     if (GetTextExtentPoint(hDC, "123,123", 8, &size))
  260.         ptArray[1] = ptArray[2] - (size.cx) - cSpaceInBetween;
  261.     else
  262.         ptArray[1] = 0;
  263.  
  264.     if (GetTextExtentPoint(hDC, "Cursor Pos:", 12, &size))
  265.         ptArray[0] = ptArray[1] - (size.cx) - cSpaceInBetween;
  266.     else
  267.         ptArray[0] = 0;
  268.  
  269.     ReleaseDC(hwndParent, hDC);
  270.  
  271.     SendMessage(hWndStatusbar,
  272.                 SB_SETPARTS,
  273.                 sizeof(ptArray)/sizeof(ptArray[0]),
  274.                 (LPARAM)(LPINT)ptArray);
  275.  
  276.     UpdateStatusBar(SZDESCRIPTION, 0, 0);
  277.     UpdateStatusBar("Cursor Pos:", 1, SBT_POPOUT);
  278.     UpdateStatusBar("Time:", 3, SBT_POPOUT);
  279. }
  280.  
  281.  
  282. //
  283. //  FUNCTION: CreateSBar(HWND, UINT, WPARAM, LPARAM)
  284. //
  285. //  PURPOSE:  Calls CreateStatusWindow() to create the status bar
  286. //
  287. //
  288. //  PARAMETERS:
  289. //
  290. //  hwndParent - Window handle of the status bar's parent
  291. //
  292. //  RETURN VALUE:
  293. //
  294. //  If both controls were created successfully Return TRUE,
  295. //  else returns FALSE.
  296. //
  297. //  COMMENTS:
  298. //
  299. //
  300.  
  301. BOOL CreateSBar(HWND hwndParent)
  302. {
  303.     hWndStatusbar = CreateStatusWindow(WS_CHILD | WS_VISIBLE | WS_BORDER,
  304.                                        SZDESCRIPTION,
  305.                                        hwndParent,
  306.                                        IDM_STATUSBAR);
  307.     if(hWndStatusbar)
  308.     {
  309.         InitializeStatusBar(hwndParent);
  310.         SetTimer(hwndParent, IDM_TIMER, TIMER_TIMEOUT, NULL);
  311.         return TRUE;
  312.     }
  313.  
  314.     return FALSE;
  315. }
  316.  
  317.  
  318. //
  319. //  FUNCTION: UpdateStatusBar(HWND)
  320. //
  321. //  PURPOSE:  Updates the statusbar control with appropriate text
  322. //
  323. //
  324. //  PARAMETERS:
  325. //
  326. //  lpszStatusString - text to be displayed
  327. //  partNumber       - which part of the status bar to display text in
  328. //  displayFlags     - display flags
  329. //
  330. //
  331. //  RETURN VALUE: NONE
  332. //
  333. //
  334. //  COMMENTS:
  335. //      None
  336. //
  337. //
  338.  
  339. void UpdateStatusBar(LPSTR lpszStatusString, WORD partNumber, WORD displayFlags)
  340. {
  341.     SendMessage(hWndStatusbar,
  342.                 SB_SETTEXT,
  343.                 partNumber | displayFlags,
  344.                 (LPARAM)lpszStatusString);
  345. }
  346.